import Vue from 'vue'
import App from './App.vue'
import './registerServiceWorker'
import router from './router'
import store from './store'
import ElementUI, {Message} from 'element-ui';
import 'element-ui/lib/theme-chalk/index.css';
import axios from 'axios'
import VueAxios from 'vue-axios'
//axios.defaults.baseURL="http://localhost:8003/system-server/"
axios.defaults.baseURL="http://localhost:8004/"
//axios.defaults.baseURL="http://localhost:8005/"
Vue.use(VueAxios, axios)
Vue.use(ElementUI);
Vue.config.productionTip = false


// 请求拦截
// 所有的网络请求都会走这个方法,可以在请求添加自定义内容
axios.interceptors.request.use(
    function (config) {
        // 请求头添加token值
        config.headers.token = sessionStorage.getItem("token")
        console.log("添加token到请求头")
        //请求头添加info值
        //config.headers.info = ''
        return config
    }
)
// 添加响应拦截器
axios.interceptors.response.use(function (res) {
    // 对响应数据做点什么
    if (res.data.code !== 200) {
        Message.error(res.data.msg)
    }
    return res;

}, function (error) {
    // 对响应错误做点什么
    return Promise.reject(error);
});

//导航守卫 防止未携带token 进入系统
router.beforeEach((to, from, next) => {
    //获取token
    let isAuthenticated = sessionStorage.getItem('token');
    //如果没有登录并且目标路由不是登录则导航到登录页面
    if (to.name !== 'login' && !isAuthenticated) {
        next({
            name: 'login' //跳转到登录页面·
        })
    }
    console.log("判断：" + (isAuthenticated && to.name !== 'login' && router.getRoutes().length === 3))
    //如果已经登录
    if (isAuthenticated && to.name !== 'login' && router.getRoutes().length == 3) {
        //  获取到一级菜单
        let menuDate = JSON.parse(sessionStorage.getItem('menus'))
        console.log("菜单列表：" + menuDate)
        menuDate.forEach(menu => {
            //二级菜单
            if (menu.hasOwnProperty("children")) {
                menu.children.forEach(subMenu => {
                    let newRouter = {
                        path: subMenu.path,
                        name: subMenu.name,
                        component: (resolve) => require([`@/views/${subMenu.component}`], resolve),
                    }
                    console.log("component : " + subMenu.component)
                    router.options.routes[1].children.push(newRouter);
                    console.log("添加动态路由：" + newRouter.path)
                    if (subMenu.hasOwnProperty("children")) {
                        subMenu.children.forEach(subMenu3 => {
                            let newRouter = {
                                path: subMenu3.path,
                                name: subMenu3.name,
                                component: (resolve) => require([`@/views/${subMenu3.component}`], resolve),
                            }
                            console.log("component : " + subMenu3.component)
                            router.options.routes[1].children.push(newRouter);
                            console.log("添加动态路由：" + newRouter.path)
                        })
                    }

                })
            }
        })
        router.selfaddRoutes(router.options.routes);
    //    等待最终添加完路由后再进行跳转
        next({...to, replace: true})
    }
    next();
})


new Vue({
    router,
    store,
    render: h => h(App)
}).$mount('#app')
